 
package com.wtzz.police.base.controller;

import com.wtzz.police.base.model.enums.WtzzResponse;
import com.wtzz.police.base.model.response.ResponseBean;
import com.wtzz.police.base.util.WebTool;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**  
* 系统出错时，返回的内容  /error  
*/
@ApiIgnore
@RestController
public class BasicErrorController implements ErrorController  {

	private static final String ERROR_PATH="/error";
	
	
	@RequestMapping("/error")
	public ResponseBean<Object> handleError(HttpServletRequest request){
		//获取statusCode:401,404,500
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		if(statusCode==404){
			return new ResponseBean<>(false,null, WtzzResponse.NOT_FOUND);
		}else {
			if(WebTool.getCurrentUser()==null)
				return new ResponseBean<>(false,null,WtzzResponse.AUTHENTICATION_FAILED);
			return new ResponseBean<>(false,null,WtzzResponse.METHOD_NOT_ALLOWED);
		}
 
	}

	@Override
	public String getErrorPath() {
		return ERROR_PATH;
	}

}
